package com.ca.ui.core.view.button;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.ca.ui.core.R;
import com.ca.ui.core.util.NumberUtils;

public class CountValueButton extends ConstraintLayout {

    private long value;
    private String text;

    public TextView valueView;
    public TextView textView;

    public CountValueButton(@NonNull Context context) {
        super(context);
        initView(context, null);
    }

    public CountValueButton(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public CountValueButton(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {

        View view = LayoutInflater.from(context).inflate(R.layout.count_value_button_layout, this);

        valueView = view.findViewById(R.id.tv_value);
        textView = view.findViewById(R.id.tv);

        if (attrs != null) {
            TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CountValueButton, 0, 0);

            value = array.getInteger(R.styleable.CountValueButton_cvb_value, 0);
            text = array.getString(R.styleable.CountValueButton_cvb_text);

            array.recycle();
        }

        setValue(value);
        setText(text);

        setOnClickListener(v -> {});
    }

    public long getValue() {
        return value;
    }

    public void setValue(long value) {
        this.value = value;
        valueView.setText(NumberUtils.numberToString((long) value));
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text == null ? "" : text;
        textView.setText(this.text);
    }
}
